package com.xxwy.springboot.webflux;

import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;
import reactor.core.publisher.Flux;

/**
 * @author xxwy
 * on 2018/8/16 0016
 */
public class ReactorDemo {

    public static void main(String[] args) {
        //reactor = jdk 8 stream + jdk 9 Reactive strem
        //Mono 0-1元素
        //Flux 0-N元素
        String[] strings = {"1","2","3"};

        //订阅者
        Subscriber<Object> subscriber = new Subscriber<Object>() {

            private Subscription subscription;

            @Override
            public void onSubscribe(Subscription subscription) {
                this.subscription = subscription;
            }

            @Override
            public void onNext(Object o) {
                System.out.println("接收数据" + o);
                this.subscription.request(1);
            }

            @Override
            public void onError(Throwable throwable) {
                throwable.printStackTrace();
                this.subscription.cancel();
            }

            @Override
            public void onComplete() {
                System.out.println("over");
            }
        };
        //发布者
        Flux.fromArray(strings).map(s -> Integer.parseInt(s))
                .subscribe(subscriber);

    }
}
